package ag.carnot.pinkblob.web.jsf.beans.pinkblob.estimatepricesandmakeagreement;

import ag.carnot.web.jsf.common.structureddata.ComplexTypeWrapper;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

public class CalculateCostEstimateBean {

	private float calcResult;
	private float texterHours;
	private float designerHours;
	private float salesHours;

	private static org.apache.log4j.Logger log = new pinkblob.log.Log()
			.getLogger(CalculateCostEstimateBean.class);

	public void complete() {

		// variables that have to be filled for completion

		// create message to display
		FacesMessage msg = new FacesMessage();
		String summary = "";

		float costCalc = (Float) getProjectInformation().get("CostCalc");

		if (costCalc < 0.1f) {
			summary = summary
					+ "Please specify a valid final cost estimation (>0)!";
		}
		if (!(summary.equals(""))) {
			log.error(summary);
			msg.setSummary(summary);
			throw new ValidatorException(msg);
		}
	}

	private ComplexTypeWrapper costTableWrapper = new ComplexTypeWrapper(
			"CostTable", null);

	public ComplexTypeWrapper getCostTableWrapper() {
		return costTableWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getCostTable() {
		return (Map) costTableWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setCostTable(Map costTable) {
		costTableWrapper = new ComplexTypeWrapper(costTableWrapper
				.getDataMappingId(), costTable);
	}

	private ComplexTypeWrapper projectInformationWrapper = new ComplexTypeWrapper(
			"ProjectInformation", null);

	public ComplexTypeWrapper getProjectInformationWrapper() {
		return projectInformationWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getProjectInformation() {
		return (Map) projectInformationWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setProjectInformation(Map projectInformation) {
		projectInformationWrapper = new ComplexTypeWrapper(
				projectInformationWrapper.getDataMappingId(),
				projectInformation);
	}

	public void setCalcResult(float result) {
		this.calcResult = result;
	}

	@SuppressWarnings("unchecked")
	public float getCalcResult() {
		Map costTable = this.getCostTable();
		Float texterRate = (Float) costTable.get("TexterWageRate");
		Float designerRate = (Float) costTable.get("DesignerWageRate");
		Float salesRate = (Float) costTable.get("SalesWageRate");
		calcResult = texterRate * texterHours + designerRate * designerHours
				+ salesRate * salesHours;

		return calcResult;
	}

	public void setTexterHours(float th) {
		texterHours = th;
	}

	public float getTexterHours() {
		return texterHours;
	}

	public void setDesignerHours(float dh) {
		designerHours = dh;
	}

	public float getDesignerHours() {
		return designerHours;
	}

	public void setSalesHours(float sh) {
		salesHours = sh;
	}

	public float getSalesHours() {
		return salesHours;
	}

}
